<template>
  <a-drawer
    title="新增维保任务"
    :maskClosable="true"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="workOrderAddVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='任务类型' v-bind="formItemLayout">
        <a-select
          :allowClear="true"
          v-model="workOrder.type"
          style="width: 100%"
          v-decorator="['role',{rules: [{ required: true, message: '请选择任务类型' }]}]">
          <a-select-option v-for="t in types" :key="t.id">{{t.name}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='任务描述' v-bind="formItemLayout">
        <a-input style="width: 100%"
                        v-model="workOrder.name"
                        v-decorator="['name',
                   {rules: [
                    { required: true, message: '任务描述不能为空'},
                    { max: 50, message: '长度不能超过50个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='所属项目' v-bind="formItemLayout">
        <a-input v-model="workOrder.project"
                 v-decorator="['project',
                   {rules: [
                    { required: true, message: '项目名称不能为空'},
                    { max: 50, message: '长度不能超过50个字符'}
                  ]}]"/>
      </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button @click="onClose" style="margin-right: .8rem">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'WorkOrderAdd',
  props: {
    workOrderAddVisible: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      workOrder: {
        name: '',
        project: ''
      },
      types: [
        {id: 0, name: '火警'},
        {id: 1, name: '故障'},
        {id: 2, name: '报修'},
        {id: 3, name: '巡查'},
        {id: 4, name: '保养'}],
      validateStatus: '',
      help: ''
    }
  },
  methods: {
    reset () {
      this.loading = false
      this.validateStatus = this.help = ''
      this.workOrder = {name: '', project: ''}
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          this.$post('workorder/add', {
            ...this.workOrder
          }).then(() => {
            this.reset()
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
